﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using ActiveHomeScriptLib;

namespace SkeletalTracking
{
    class ActiveHomeBrightnessThread
    {
        
        const int DELAY = 100;
        const int BRIGHTNESS_CHANGE_DELAY = 2000;
        private String device;

        private Thread baseThread;

        private bool end = false;

        int brightness;

        //Active home
        private ActiveHomeClass activeHome = new ActiveHomeClass();

        public ActiveHomeBrightnessThread(String device, int defaultBrightness){
            this.device = device;
            brightness = defaultBrightness;
            baseThread=new Thread(run);
            baseThread.Start();
        }

        public void endThread(){
            end = true;
            baseThread.Join();
        }

        private void run()
        {
            bool brightnessChanging = false;
            Object test;
            while (!end)
            {

                lock (this)
                {
                    int currentBrightness = Convert.ToInt16(activeHome.SendAction("QueryPLC", device + " Dim"));
                    if (currentBrightness != -1)//error
                    {
                        if (!brightnessChanging)
                        {
                            if (brightness > currentBrightness)
                                test = activeHome.SendAction("SendPLC", device + " Bright " + (brightness - currentBrightness));
                            else if (brightness < currentBrightness)
                                test = activeHome.SendAction("SendPLC", device + " Dim " + (currentBrightness - brightness));
                        }
                        if (brightness != currentBrightness)
                        {
                            brightnessChanging = true;
                        }
                        else
                        {
                            brightnessChanging = false;
                        }
                    }
                }

                if (brightnessChanging)
                {
                    Thread.Sleep(BRIGHTNESS_CHANGE_DELAY);
                    brightnessChanging = false;
                }
                else
                {
                    Thread.Sleep(DELAY);
                }
            }
            activeHome.SendAction("SendPLC", device + " Off");
        }
       
       
        /// <summary>
        /// Safely sets the brightness to the specified percentage
        /// </summary>
        /// <param name="newBrightness">int: sets the brightness to a specified value </param>
        public void setBrightness(int newBrightness){
            lock (this)
            {
                brightness = newBrightness;
            }
        }
        /// <summary>
        /// turns the lamp on
        /// </summary>
        public void turnLampOn()
        {
            lock (this)
            {
                activeHome.SendAction("SendPLC", device + " On");
            }
        }


        internal void turnLampOff()
        {
            activeHome.SendAction("SendPLC", device + " Off");
        }
    }
}
